package com.twentytwok.hospitalsystem22K.controller;

import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.common.Code;
import com.twentytwok.hospitalsystem22K.common.PieBo;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.pojo.Drug;
import com.twentytwok.hospitalsystem22K.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hospital
 * @description: 药物管理控制层
 * @author: Mr.Wu
 * @create: 2021-11-09 11:32
 **/
@CrossOrigin("*")
@RequestMapping("/api/drug")
@RestController
public class DrugController {

    @Autowired
    private DrugService drugService;

    @RequestMapping("/findAll")
    public R findAllDrug(@RequestParam Map<String, Object> map) {
        PageInfo<Drug> allDrug = drugService.findAllDrug(map);

        return new R(Code.OK, "sdf", (int) allDrug.getTotal(), allDrug.getList());
    }

    @RequestMapping("/insert")
    public R insertDrug(@RequestParam Map<String, Object> map) {
        int result = drugService.insertDrug(map);
        if (result == 0) {
            return new R(Code.ERROR, "添加失败");
        } else {
            return new R(Code.OK, "添加成功");
        }
    }

    @RequestMapping("/update")
    public R updateDrug(@RequestParam Map<String, Object> map) {
        int result = drugService.updateDrug(map);
        if (result == 0) {
            return new R(Code.ERROR, "修改失败");
        } else {
            return new R(Code.OK, "修改成功");
        }
    }

    @RequestMapping("/delete")
    public R deleteDrug(Integer id, HttpServletRequest request) {
        int result = drugService.deleteDrug(id);
        if (result == 0) {
            return new R(Code.ERROR, "删除失败");
        } else {
            return new R(Code.OK, "删除成功");
        }
    }

    @RequestMapping("/findAllDrugs")
    public R findAllDrugs(String key) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", key);

        PageInfo<Drug> allDrug = drugService.findAllDrug(map);
        return new R(Code.OK, "查询成功", (int) allDrug.getTotal(), allDrug.getList());
    }

    @RequestMapping("/pie")
    public R pie(String key) {
        List<PieBo> list = drugService.pie();

        return new R(Code.OK, "查询成功", 1, list);
    }

}
